package com.v.login;

import com.Dao.StudiosDao;
import com.entity.Studios;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Movie extends JFrame {
    //划分面板，

    private JPanel jp_north;//北部面板
    private JLabel jl_cinema;//影院文本
    private JTextField jtf_cinema;//输入影院文本域
    private JButton jb_search;//搜索
    private JButton jb_return;//返回上一层
    private JButton jb_selection;//选座购票


    private Object[] name = {"场次编号","场厅名称","影院名称","影院地址","电影名称","开始时间","价格","剩余座位数"};

    private JTable jt_showDate;//显示数据的表格
    public JTable getJt_showDate() {
        return jt_showDate;
    }

    private Map<Integer, List<Studios>> Map=new HashMap<>();

    private JScrollPane jsp_south;//南部面板


    private List<Studios> list = new ArrayList<>();
    public List<Studios> getPnList(){

        return list;
    }

    private int pn;//当前页

    private  Studios studios = new Studios();
    public static void main(String[] args) {
        new Movie();
    }

    //初始化界面
    public Movie() {




        //北部面板
        jp_north = new JPanel();
        jp_north.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 25));//设置网格布局
        jp_north.setPreferredSize(new Dimension(0, 70));
        add(BorderLayout.NORTH,jp_north);

        //影院
        jl_cinema = new JLabel("影院：",JLabel.CENTER);
        jl_cinema.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        jp_north.add(jl_cinema);

        //影院搜索框
        jtf_cinema = new JTextField(8);
        jtf_cinema.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jp_north.add(jtf_cinema);

        jb_search = new JButton("搜索");
        jb_search.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        jp_north.add(jb_search);

        jb_return = new JButton("返回上一层");

        jb_return.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jp_north.add(jb_return);

        jb_selection = new JButton("选座购票");
        jb_selection.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jp_north.add(jb_selection);



        //创建一个表格
        Object[][] data = {{1,2,1,4,3,5,6,8,9},{2,2,1,4,3,5,6,8,9}};
        jt_showDate = new JTable();
        JTableHeader header = jt_showDate.getTableHeader();
        header.setFont(new Font("楷体", Font.PLAIN, 16));
        header.setForeground(Color.MAGENTA);
        jt_showDate.setModel(new DefaultTableModel(data,name));
        //  设置单元格的样式

        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        //表格内容居中
        r.setHorizontalAlignment(JLabel.CENTER);
        jt_showDate.setDefaultRenderer(Object.class, r);
        jt_showDate.setRowHeight(30);//设置表格高度
        //设置表格字体样式
        jt_showDate.setFont(new Font("楷体", Font.PLAIN, 16));



        //南部面板
        jsp_south = new JScrollPane(jt_showDate);
        jsp_south.setBorder(new LineBorder(Color.GREEN,2));//边框颜色
        jsp_south.setPreferredSize(new Dimension(0,480));
        add(jsp_south, BorderLayout.SOUTH);

        //显示数据
        //showStudios();



        setTitle("想看电影");
        setResizable(false);
        setSize(1000, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

    }

    public Object[][] listToArray(List<Studios> studios){


        Object[][] data = new Object[studios.size()][];
        for (int i = 0; i < studios.size(); i++) {
            Studios studios1 = studios.get(i);
            //每一行数据的长度与与表头一直
            Object[] userData = {studios1.getId(),studios1.getHallName(),studios1.getCinemaName()
                       ,studios1.getCinemaAddress(),studios1.getMovieTitle(),studios1.getStartTime()
                       ,studios1.getPrice(),studios1.getSurplus()};
            //将一维数组放入到二维数组
            data[i] = userData;

        }

        return data;
    }

    public void showStudios(){

        try {
            //list = Map.get(pn);

            StudiosDao studiosDao = new StudiosDao();
            List<Studios> list1 = studiosDao.queryStudios();

            Object[][] data = listToArray(list1);
            jt_showDate.setModel(new DefaultTableModel(data,name));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
